package com.facecard;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class ManagerContactsList extends Activity {
    // Debugging
    private static final String TAG = "BluetoothChat";
    private static final boolean D = true;

    // Intent request codes
    private static final int REQUEST_CONFIG = 1;
    private static final int REQUEST_OWNER_INFO = 2;
    private static final int REQUEST_BLUETOOTH_RANGE_LIST = 3;
    private static final int REQUEST_HELP = 4;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_config:
/*			Intent intent = new Intent(ManagerContactsList.this.getBaseContext(), FileOpenDialog.class);
			intent.putExtra(FileOpenDialog.START_PATH, "/sdcard");
			ManagerContactsList.this.startActivityForResult(intent, 3);
*/			
            // Launch the preference
            Intent configIntent = new Intent(this, Config.class);
            startActivityForResult(configIntent, REQUEST_CONFIG);
            return true;
        case R.id.menu_owner_info:
            // Launch the preference
            Intent ownerInfoIntent = new Intent(this, OwnerInfo.class);
            startActivityForResult(ownerInfoIntent, REQUEST_OWNER_INFO);
            return true;
        case R.id.menu_chat:
            // Launch the DeviceListActivity to see devices and do scan
            //Intent bluetoothRangeList = new Intent(this, DeviceListActivity.class);
            Intent chatIntent = new Intent(this, BluetoothChat.class);
            startActivityForResult(chatIntent, REQUEST_BLUETOOTH_RANGE_LIST);
            return true;
        case R.id.menu_bluetoothrangelist:
            // Launch the DeviceListActivity to see devices and do scan
            Intent bluetoothRangeList = new Intent(this, BluetoothRangeList.class);
            startActivityForResult(bluetoothRangeList, REQUEST_BLUETOOTH_RANGE_LIST);
            return true;
        case R.id.menu_help:
            // Launch the DeviceListActivity to see devices and do scan
            Intent helpIntent = new Intent(this, Help.class);
            startActivityForResult(helpIntent, REQUEST_HELP);
            return true;
        }
        return false;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(D) Log.d(TAG, "onActivityResult " + resultCode);
        switch (requestCode) {
        case REQUEST_CONFIG:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
/*                // Get the device MAC address
                String address = data.getExtras()
                                     .getString(Config.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                // Attempt to connect to the device
                mChatService.connect(device);
*/            }
            break;
        case REQUEST_BLUETOOTH_RANGE_LIST:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
/*                // Bluetooth is now enabled, so set up a chat session
                setupChat();
            } else {
                // User did not enable Bluetooth or an error occured
                Log.d(TAG, "BT not enabled");
                Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
                finish();*/
            }
        }
    }

    private void ensureDiscoverable() {
        if(D) Log.d(TAG, "ensure discoverable");
        if (BluetoothAdapter.getDefaultAdapter().getScanMode() !=
            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
            startActivity(discoverableIntent);
        }
    }
}
